var Passport = function ()
{
	var passport_not_exists    = true;
	var passport_capture_valid = false;
	var nickname_is_valid      = false;
	var login_ru               = null;

	this.register = function(form) 
	{
		var userid = form.userid.value;
		var passwd = form.password.value;
		var mobile = form.mobile.value;
		var passwd2= form.re_password.value;
		var name   = form.realname.value;
		var code   = form.captcha.value;
		var atype  = form.account_type.value;
		var objDate = new Date();
	
		if (this.check_login_userid(form.userid) && 
		    this.check_reg_passwd(form.password, form.re_password) &&
		    this.check_reg_mobile(form.mobile) && 
		    this.check_reg_realname(form.realname) &&
		    this.passport_capture_valid )
		{
		}
		else
		{
			return false;
		}
		
		var errormsg = $('#errormsg');
		errormsg.html('注册中。。。');
		errormsg.show();
			
		$.post('/passport/handle_register', { 
			'userid' : userid, 
			'passwd' : passwd, 
			'mobile' : mobile,
			'realname': name,
			'checkcode': code,
			'type'   : atype,
			'ext' : objDate.getTime() },	function (o) {
	
			if (o.status == 'EXISTS')
			{
				errormsg.html('您的邮件已存在，请重新注册');
				return false;
			}
			else if (o.status != 'VALID')
			{
				errormsg.html(o.message);
				//errormsg.html('系统内部错误，请等待管理员处理');
				return false;
			}
			else
			{
            			window.location.assign('/admin');
				return false;
			}

			}, 'json');
	
		return false;

	}, // end function register


	this.login = function(form, login_ru) 
	{
		var userid = form.userid.value;
		var passwd = form.password.value;
		var ru = form.ru.value;
		var objDate = new Date();
		this.login_ru = login_ru;
	
		if (this.check_login_userid(form.userid) && 
		    this.check_login_passwd(form.password))
		{
		}
		else
		{
			return false;
		}
		
		var regmobile = /^1\d{10}$/;
		if (regmobile.test(userid))
		{
			var errormsg = document.getElementById('errormsg');
			var errormsg_text = document.getElementById('errormsg_text');
			errormsg.style.display = 'block';
			errormsg_text.innerHTML = '登录中。。。';
			$.get('/passport/ajax/auth', { 'mobile' : userid, 'passwd' : passwd, 'ext' : objDate.getTime() },	function (o) {
			if (o.status != 'VALID')
			{
				var errormsg = document.getElementById('errormsg');
				var errormsg_text = document.getElementById('errormsg_text');
				errormsg.style.display = 'block';
				errormsg_text.innerHTML = '该手机没有注册或者密码错误，请检查后重新登录！';
				return false;
			}

			if(o.status == 'CLOSED')
			{
				var errormsg = document.getElementById('errormsg');
				var errormsg_text = document.getElementById('errormsg_text');
				errormsg.style.display = 'block';
				errormsg_text.innerHTML = '店铺已关闭，请联系管理员！';
				return false;
			}
	
			userid = o.userid;
			var passport_url = 'http://passport.sohu.com/sso/login_js.jsp';
			var passport_params = {
				'userid'       : userid,
				'password'     : passwd,
				'appid'        : 1028,
				'persistentcookie' : 1,
				'pwdtype'      : 0,
				't'            : objDate.getTime(),
				'ru'           : passport.login_ru 
			};
	
			tuan_post(passport_url, passport_params);
			return false;
			}, 'json');
		}
		else
		{
			var errormsg = $('#errormsg');
			errormsg.show();
			errormsg.html('登录中。。。');
			
			$.get('/passport/ajax/auth', { 'userid' : userid, 'passwd' : passwd, 'ext' : objDate.getTime() },	function (o) {
	
			if (o.status == 'INACTIVE')
			{
				errormsg.html('您的账户尚未激活，请激活后再登录！未收到激活邮件？<a href=/passport/send_mail?passport=' + userid + '&passwd=' + o.passwd + '&nickname=' + userid + '>重新发送！</a>');
				return false;
			}
			else if(o.status == 'CLOSED')
			{
				errormsg.html('账户已关闭，请联系管理员！');
				return false;
			}
			else if (o.status != 'VALID')
			{
				errormsg.html('用户名不存在或者密码错误，请检查后重新登录！');
				return false;
			}
			
            window.location.assign('/admin');
			return false;
			}, 'json');
	
		}

		return false;

	}, // end function login


	this.check_login_userid = function(obj) 
	{
		var msg     = $('#errormsg');
	
		if (obj.value == '')
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('登录名不能为空');
			msg.show();
			return false;
		}
		else if (! /^.+@.+$/.test(obj.value) && !/^1\d{10}$/.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('登录名格式错误');
			msg.show();
			return false;
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
		}
	}, // end function check_login_userid

	this.check_reg_mobile = function(obj) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*1\d{10}\s*$/;
	
		if (pattern.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
	
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('手机格式错误');
			msg.show();
			return false;
		}
	}, // end function check_login_passwd


	this.check_reg_realname = function(obj) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*[^\s]{2,}\s*$/;
	
		if (pattern.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
	
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('姓名格式错误');
			msg.show();
			return false;
		}
	}, // end function check_login_passwd



	this.check_reg_password = function(obj) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*[^\s]{6,16}\s*$/;
	
		if (pattern.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
		}
		else if (obj.value == '')
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('密码不能为空');
			msg.show();
			return false;
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('密码格式错误');
			msg.show();
			return false;
		}
	}, // end function check_login_passwd

	this.check_reg_captcha = function(obj) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*[^\s]{3,8}\s*$/;
	
		if (pattern.test(obj.value))
		{
			$.get('/passport/ajax/checkcode', {'code' : obj.value}, function (o) {
					if (o && o.status == 0) {
			passport.passport_capture_valid = true;
			$(obj).parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
					} else {

			passport.passport_capture_valid = false;
			$(obj).parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('验证码错误，请重新输入');
			msg.show();
			return false;
					}
				}, 'json');
		}
		else if (obj.value == '')
		{
			$(obj).parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('验证码不能为空');
			msg.show();
			return false;
		}
		else
		{
			$(obj).parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('验证码格式错误');
			msg.show();
			return false;
		}
	}, // end function check_login_passwd


	this.check_reg_passwd = function(obj, obj2) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*[^\s]{6,16}\s*$/;
	
		if (pattern.test(obj.value) && pattern.test(obj2.value) && 
			obj.value == obj2.value)
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			$(obj2).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
	
		}
		else if (!pattern.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('密码格式错误');
			msg.show();
			return false;
		}
		else if (!pattern.test(obj2.value))
		{
			$(obj2).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('密码格式错误');
			msg.show();
			return false;
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			$(obj2).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('两次密码输入不一致');
			msg.show();
			return false;
		}
	}, // end function check_login_passwd



	this.check_login_passwd = function(obj) 
	{
		var msg     = $('#errormsg');
		var pattern = /^\s*[^\s]{6,16}\s*$/;
	
		if (pattern.test(obj.value))
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/success-btn.png">');
			msg.html('');
			msg.hide();
			return true;
	
		}
		else
		{
			$(obj).parent().parent().parent().find('.tip-image:first').html('<img src="/html/images/error-btn.png">');
			msg.html('密码格式错误');
			msg.show();
	
			return false;
		}
	}, // end function check_login_passwd


	this.email_notfrom_sohu = function() 
	{
		var mail = $('#email').val();
		var pattern = /^.+@sohu\.com\s*$/;
		if (pattern.test(mail)) 
			return false;
		else
		{
			return true;
		}

	}, // end of function email_notfrom_sohu

	this.passport_valid = function(mail) 
	{
		return false;
	}, //end of function email_valid


	this.ihome_checkEmail=function(obj) 
	{
		var mail    = obj.value;
		var mail_pattern = /^[a-z0-9\.\-_]{4,32}@.+$/;
		var valid_mail = false;

		if (mail_pattern.test(mail))
		{
			var maddrs = mail.split('@');
			prefix = maddrs[0];
			suffix = maddrs[1];

			if (/^[0-9a-z\.\-_]{4,32}$/.test(prefix))
			{
				valid_mail = true;
			}
		}

		if (! valid_mail)
		{
			var tip = document.getElementById("check_email");
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-err';
			tip.innerHTML = '邮件格式错误';
			return false;
		}
		else 
		{
			// check if sohu passport
			var pattern = /^.+@sohuuuuuu\.com\s*$/;
			if (pattern.test(mail)) {
	
				var tip = document.getElementById("check_email");
	
				obj.parentNode.parentNode.className = 'reg-form';
				tip.className = 'check-ok';
				tip.innerHTML = '将为您注册搜狐信箱';
				return false;
			} else {
				var time = new Date().getTime();
				$.get('/passport/ajax/email/?now=' + time, { 'email' : mail }, function (o) { 
						if (o && o.status != 0 && o.status != 4)
						{
							passport.passport_not_exists = false;
							var tip = document.getElementById("check_email");
							obj.parentNode.parentNode.className = 'reg-form';
							tip.className = 'check-err';
							tip.innerHTML = '格式错误';
						}
						else if (o && o.status == 4)
						{
							passport.passport_not_exists = false;
							var tip = document.getElementById("check_email");
							obj.parentNode.parentNode.className = 'reg-form';
							tip.className = 'check-err';
							tip.innerHTML = '已被注册，<a href="/passport/login">请直接登陆</a>';
						} else {
							passport.passport_not_exists = true;
							var tip = document.getElementById("check_email");
							obj.parentNode.parentNode.className = 'reg-form';
							tip.className = 'check-ok';
							tip.innerHTML = '&nbsp;';
						}
	
		
					}, 'json');
			}
	
			return true;
		}
	
		return false;

	}, // end of function ihome_checkEmail


	this.ihome_accountSubmit = function(obj) 
	{
		var agree = document.getElementById('agree');
		if (! agree.checked)
		{
			alert('您还没有同意应用汇网络服务协议');
			return false;
		}
	
		var form = document.forms['form_register'];
		if (!form) 
			return false;
	
		if (this.ihome_checkEmail(form.email) && 
		    this.ihome_checkPasswd1(form.password1) &&
		    this.ihome_checkPasswd2(form.password2) &&
			this.nickname_is_valid &&
		    this.passport_capture_valid &&
		    this.passport_not_exists
		    )
		{
			return true;
		}
		return false;

	}, // end of function ihome_accountSubmit

	this.bytelength = function(str) 
	{
		return str.replace(/[^\x00-\xff]/g, '**').length;
	}, // end of function bytelength


	this.limitNickname = function() 
	{
		var value = $('#nickname').val();
		var len = bytelength(value);
		if (len <= 16 && len >= 4)
			return true;
		else
			return false;
	}, // end of function limitNickname

	this.ihome_checkNickname = function(obj) 
	{
		var time = new Date().getTime();
		var tip  = document.getElementById("check_nickname");
		var nick = obj.value;
		$.get('/passport/ajax/nickname/?t=' + time, { 'nickname' : nick }, function (o) { 
			if (o && o.status != 0)
			{
				passport.nickname_is_valid = false;
				obj.parentNode.parentNode.className = 'reg-form';
				tip.style.display = '';
				tip.className = 'check-err';
				tip.innerHTML = o.message;
				return false;
			}
			else if (o && o.status == 0)
			{
				passport.nickname_is_valid = true;
				obj.parentNode.parentNode.className = 'reg-form';
				tip.className = 'check-ok';
				tip.innerHTML = '';
				return true;
			}
		}, 'json');
	
	}, // end of function checkNickname


	this.ihome_checkPasswd1 = function(obj) 
	{
		var tip     = document.getElementById("check_passwd1");
		var pattern = /^\s*[^\s]{6,16}\s*$/;
		if (pattern.test(obj.value))
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-ok';
			tip.innerHTML = '&nbsp;';
			return true;
		}
		else
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-err';
			tip.innerHTML = '密码格式不对';
			return false;
		}

	}, // end of function ihome_checkPasswd1 

	this.ihome_checkPasswd2 = function(obj) 
	{
		var tip     = document.getElementById("check_passwd2");
		var form    = document.forms['form_register'];
		var pwd1    = form.password1;
	
		if (obj.value == '')
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-err';
			tip.innerHTML = '确认密码不能为空';
			return false;
		}
	
		if (obj.value == pwd1.value)
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-ok';
			tip.innerHTML = '&nbsp;';
			return true;
		}
		else
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-err';
			tip.innerHTML = '密码不一致';
			return false;
		}

	}, // end of function ihome_checkPasswd2

	this.ihome_checkCapture = function(obj) 
	{
		this.passport_capture_valid = false;
		var tip     = document.getElementById("check_capture");
		var pattern = /^\s*\w{3,8}\s*$/;
		if (pattern.test(obj.value))
		{
			$.get('/passport/ajax/checkcode', {'code' : obj.value}, function (o) {
					if (o && o.status == 0) {
						passport.passport_capture_valid = true;
						obj.parentNode.parentNode.className = 'reg-form';
						tip.className = 'check-ok';
						tip.innerHTML = '&nbsp;';
					} else {
						obj.parentNode.parentNode.className = 'reg-form';
						tip.className = 'check-err';
						tip.innerHTML = '验证码错误，请重新输入';
					}
				}, 'json');
	
			return false;
		}
		else
		{
			obj.parentNode.parentNode.className = 'reg-form';
			tip.className = 'check-err';
			tip.innerHTML = '验证码格式不对';
			return false;
		}

	}, // end of function ihome_checkCapture


	this.subscript = function(obj) 
	{
		var email_subscription = obj.form.email_subscription;
		if (obj.checked)
			email_subscription.value = 1;
		else
			email_subscription.value = 0;
	}, // end of function subscript

	this.ihome_accountSubmit2 = function(obj) 
	{
		var form = document.forms['form_register'];
		if (!form) 
			return false;
	
		if (this.ihome_checkEmail(form.email) && 
		    this.ihome_checkPasswd1(form.password1) &&
		    this.ihome_checkPasswd2(form.password2) &&
		    this.passport_not_exists
		    )
		{
			return true;
		}
	
		return false;

	} // end of function ihome_accountSubmit2

} // END Package Passport


var MerchantApply = function () {

	var shopname_not_exists = true;

	this.check_shopname = function (v) {
		if (v.value.length > 3 && v.value.length <= 20)
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();

			//
			// Ajax 检测该商铺名称是否已被占用
			//
			$.get(
			'/merchantapply/ajax_valid_shopname/'+encodeURIComponent(v.value), 
					{ 'now' : new Date().getTime() }, 
					function (o) { 
						if (o && o.status == 'OK')
						{
							zx_m_apply.shopname_not_exists = true;	
							$('.ajax_tip_sname').hide();
						}
						else
						{
							zx_m_apply.shopname_not_exists = false;
							$('.ajax_tip_sname').show();
						}
					}, 'json');

			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			$('.ajax_tip_sname').hide();
			return false;
		}
	}

	this.check_location = function (province, city, county) {

		// 城市不能为空
		if (province.value > 0 && city.value > 0 )
		{
			$(city).removeClass('error');
			$(city).parent().next().hide();
			return true;
		}
		else
		{
			$(city).addClass('error');
			$(city).parent().next().show();
			return false;
		}
	}

	this.check_address = function (v) {
		if (v.value.length > 3 && v.value.length <= 30)
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			return false;
		}
	}

	this.check_contract= function (v) {
		if (v.value.length >= 2 && v.value.length <= 5)
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			return false;
		}
	}

	this.check_kefu  = function (v) {
		var regmobile = /^1\d{10}$/;
		var regphone = /^0\d{2,3}-\d{7,8}$/;
		if (regmobile.test(v.value) || regphone.test(v.value))
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			return false;
		}
	}

	this.check_email  = function (v) {
		var regemail = /^.+@.+\..+$/;
		if (regemail.test(v.value))
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			return false;
		}
	}


	this.check_mobile  = function (v) {
		var regmobile = /^1\d{10}$/;
		if (regmobile.test(v.value))
		{
			$(v).removeClass('error');
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).addClass('error');
			$(v).parent().next().show();
			return false;
		}
	}

	this.check_term  = function (v) {
		if ($(v).attr('checked'))
		{
			$(v).parent().next().hide();
			return true;
		}
		else
		{
			$(v).parent().next().show();
			return false;
		}
	}


	this.submit = function() 
	{
		var form = document.forms['merchant_apply'];
		if (!form) 
			return false;
	
		if (
			this.check_shopname (form.shop_name) &&
		    this.check_location (form.province, form.city, form.county) &&
		    this.check_address  (form.address) &&
		    this.check_kefu (form.kefu) &&
		    this.check_contract (form.name) &&
		    this.check_mobile   (form.mobile) &&
		    this.check_email   (form.email) &&
		    this.check_term     (form.tiaokuan) &&
		    this.shopname_not_exists 
		    )
		{
			return true;
		}
		else
		{
			return false;
		}

	} // end of function submit
}
